package com.fengxing.ams.pub.util;

import java.util.List;
import java.util.Map;

import org.apache.http.client.HttpClient;

/**
 * A utility class for accessing a url via http protocol.
 */
public class HttpClientUtils {
    private static HttpClientUtils2 instance = HttpClientUtils2.getInstance();
    
    /**
     * Reset connection timeout and socket timeout for new http connections.
     * @param timeout the timeout value in millisecond.
     */
    public static void setTimeout(int timeout) {
        instance.setTimeout(timeout);
    }
    
    /**
     * Return a new httpClient using default configuration parameters.
     * @return A new HttpClient object.
     */
    public static HttpClient getHttpClient() {
    	return instance.getHttpClient();
    }
    
    /**
     * Send a http 'get' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url. 
     */
    public static String get(String url) {
    	TecHttpResponse response = instance.get(url);
        return response.getResult();
    }
    
    /**
     * Send a http 'get' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url. 
     */
    public static String get(String url, boolean needEncodeUrl) {
    	TecHttpResponse response = instance.get(url, needEncodeUrl);
        return response.getResult();
    }
    
    /**
     * Send a http 'get' request to given url and return the response content.
     * If the response status is not OK(200), HttpClientException would be thrown.
     * @param url the url that the request will be sent to.
     * @throws HttpClientException thrown when the IO exception occurs or the response status is not OK(200).
     * @return the response content got from the specified url. 
     */
    public static String getStrictly(String url) {
    	TecHttpResponse response = instance.getStrictly(url);
        return response.getResult();
    }
    
    /**
     * Send a http 'get' request to given url and return the response content.
     * If the response status is not OK(200), HttpClientException would be thrown.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @throws HttpClientException thrown when the IO exception occurs or the response status is not OK(200).
     * @return the response content got from the specified url. 
     */
    public static String getStrictly(String url, boolean needEncodeUrl) {
    	TecHttpResponse response = instance.getStrictly(url, needEncodeUrl);
        return response.getResult();
    }
    
    /**
     * Send a http 'get' request to given url and return the response content.
     * it uses the apache http single thread model for the connection management. 
     * @param url the url that will send request to.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return  
     */
    public static String getWithSingleThread(String url) {
    	TecHttpResponse response = instance.getWithSingleThread(url);
        return response.getResult();
    }
    
    /**
     * Send a http 'get' request to given url and return the response content.
     * it uses the apache http single thread model for the connection management. 
     * @param url the url that will send request to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return  
     */
    public static String getWithSingleThread(String url, boolean needEncodeUrl) {
        TecHttpResponse response = instance.getWithSingleThread(url, needEncodeUrl);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String get(String url, Map<String, String> paramsMap) {
    	TecHttpResponse response = instance.get(url, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String get(String url, boolean needEncodeUrl, Map<String, String> paramsMap) {
    	TecHttpResponse response = instance.get(url, needEncodeUrl, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String get2(String url, Map<String, List<String>> paramsMap) {
    	TecHttpResponse response = instance.get2(url, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String get2(String url, boolean needEncodeUrl, Map<String, List<String>> paramsMap) {
    	TecHttpResponse response = instance.get2(url, needEncodeUrl, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'get' request to given url and return the response content.
     * If the response status is not OK(200), HttpClientException would be thrown.
     * @param url the url that the request will be sent to.
     * @throws HttpClientException thrown when the IO exception occurs or the response status is not OK(200).
     * @return the response content got from the specified url. 
     */
    public static String getStrictly(String url, Map<String, String> paramsMap) {
    	TecHttpResponse response = instance.getStrictly(url, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'get' request to given url and return the response content.
     * If the response status is not OK(200), HttpClientException would be thrown.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @throws HttpClientException thrown when the IO exception occurs or the response status is not OK(200).
     * @return the response content got from the specified url. 
     */
    public static String getStrictly(String url, boolean needEncodeUrl, Map<String, String> paramsMap) {
    	TecHttpResponse response = instance.getStrictly(url, needEncodeUrl, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String get2Strictly(String url, Map<String, List<String>> paramsMap) {
    	TecHttpResponse response = instance.get2Strictly(url, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String get2Strictly(String url, boolean needEncodeUrl, Map<String, List<String>> paramsMap) {
    	TecHttpResponse response = instance.get2Strictly(url, needEncodeUrl, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String post(String url, Map<String, String> paramsMap) {
    	TecHttpResponse response = instance.post(url, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String post(String url, boolean needEncodeUrl, Map<String, String> paramsMap) {
    	TecHttpResponse response = instance.post(url, needEncodeUrl, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String post2(String url, Map<String, List<String>> paramsMap) {
    	TecHttpResponse response = instance.post2(url, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown only when the IO exception occurs.
     * @return the response content got from the specified url.
     */
    public static String post2(String url, boolean needEncodeUrl, Map<String, List<String>> paramsMap) {
    	TecHttpResponse response = instance.post2(url, needEncodeUrl, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown when the IO exception occurs or the response status is not OK(200).
     * @return the response content got from the specified url.
     */
    public static String postStrictly(String url, Map<String, String> paramsMap) {
    	TecHttpResponse response = instance.postStrictly(url, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown when the IO exception occurs or the response status is not OK(200).
     * @return the response content got from the specified url.
     */
    public static String postStrictly(String url, boolean needEncodeUrl, Map<String, String> paramsMap) {
    	TecHttpResponse response = instance.postStrictly(url, needEncodeUrl, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown when the IO exception occurs or the response status is not OK(200).
     * @return the response content got from the specified url.
     */
    public static String post2Strictly(String url, Map<String, List<String>> paramsMap) {
    	TecHttpResponse response = instance.post2Strictly(url, paramsMap);
        return response.getResult();
    }
    
    /**
     * Send a http 'post' request to given url and return the response content.
     * @param url the url that the request will be sent to.
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @param paramsMap the parameters will be sent to given url.
     * @throws HttpClientException thrown when the IO exception occurs or the response status is not OK(200).
     * @return the response content got from the specified url.
     */
    public static String post2Strictly(String url, boolean needEncodeUrl, Map<String, List<String>> paramsMap) {
    	TecHttpResponse response = instance.post2Strictly(url, needEncodeUrl, paramsMap);
        return response.getResult();
    }
    
    /**
     * @param url
     * @param requestContent
     * @param headerMap
     * @return
     */
    public static String request(String url, String requestContent, Map<String, String> headerMap) {
    	TecHttpResponse response = instance.request(url, requestContent, headerMap);
        return response.getResult();
    }
    
    /**
     * @param url
     * @param requestContent
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @param headerMap
     * @return
     */
    public static String request(String url, boolean needEncodeUrl, String requestContent, Map<String, String> headerMap) {
    	TecHttpResponse response = instance.request(url, needEncodeUrl, requestContent, headerMap);
        return response.getResult();
    }
    
    /**
     * check url status
     * @param url
     * @return 
     */
    public static int getUrlStatus(String url) {
        return instance.getUrlStatus(url);
    }
    
    /**
     * check url status
     * @param url
     * @param needEncodeUrl indicates if needs to execute encoding on the given url.
     * @return 
     */
    public static int getUrlStatus(String url, boolean needEncodeUrl) {
    	return instance.getUrlStatus(url, needEncodeUrl);
    }
}
